package com.mall.manager.schemamanage.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.util.WebUtils;

import com.mall.untils.Page;
import com.mall.untils.StringUtil;
@Service
public class CSManageService {
	private Logger logger = LoggerFactory.getLogger(CSManageService.class);
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	
	/**
	 * @功能 	: 根据方案id查询的操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public Map<String, Object>  find_message(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		String sql="select *,date_format(create_date,'%Y-%m-%d') create_date_new from db_programme.t_manage where id=?";
		try {
			Map<String, Object> queryForMap = jdbcTemplate.queryForMap(sql,id);
			logger.info("根据方案id查询成功！");
			return queryForMap;
		} catch (Exception e) {
			logger.info("根据方案id查询失败！");
		}
		return null;
	}
	
	
	/**
	 * @功能 	: 对方案表的添加和修改操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public int contentform_schemamanage_add(Map<String, Object> paraMap) {
		//添加数据时所需要的值
		int b_id = MapUtils.getIntValue(paraMap, "b_id");
		String title = MapUtils.getString(paraMap, "title");
		String vice_title = MapUtils.getString(paraMap, "vice_title");
		String create_author = MapUtils.getString(paraMap, "create_author");
		//新添加
		int direction = MapUtils.getIntValue(paraMap, "direction");
		int week = MapUtils.getIntValue(paraMap, "week");
		String age_stage = MapUtils.getString(paraMap, "age_stageses");
		int criterion_num = MapUtils.getIntValue(paraMap, "criterion_num");
		
		String content = MapUtils.getString(paraMap, "content");
		String label = MapUtils.getString(paraMap, "label");
		String label_keyword = MapUtils.getString(paraMap, "label_keyword");
		String summary = MapUtils.getString(paraMap, "summary");
		String route =MapUtils.getString(paraMap, "cover_url");
		String date = MapUtils.getString(paraMap, "date");
		try {
			int id = MapUtils.getIntValue(paraMap, "id");
			int addNum=0;
			String sql = "";
			if(id==0){
				sql = "INSERT INTO db_programme.t_manage (b_id, title, vice_title, create_author,direction,age_stage,week,criterion_num, content, label, label_keyword, summary, route, create_date,create_time,update_time) VALUES (?, ?, ?, ?,?,?,?, ?,?, ?, ?, ?, ?,?,now(),now())";
				addNum = this.jdbcTemplate.update(sql, b_id, title, vice_title,create_author,direction,age_stage,week,criterion_num, content, label, label_keyword, summary,route,date);
				if (addNum > 0) {
					logger.info("方案添加成功！");
				}
			}else{
				sql="update db_programme.t_manage set summary=?,title=?,vice_title=?,create_author=?,direction=?,age_stage=?,week=?,criterion_num=?,content=?,label=?,label_keyword=?,route=?,create_date=?,update_time=now() where id=?";
				addNum= jdbcTemplate.update(sql,summary,title,vice_title,create_author,direction,age_stage,week,criterion_num,content,label,label_keyword,route,date,id);
				if (addNum > 0) {
					logger.info("方案修改成功！");
				}
			}
			return addNum;
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("方案操作失败！【错误】:contentform_schemamanage_add方法中！");
			return 0;
		}
	}
	
	
	/**
	 * @功能 	: 对方案表的分页和模糊查询的操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public Page manager_scheme_show(HttpServletRequest request) {
		Page page = null;
	    Map<String, Object> paraMap = WebUtils.getParametersStartingWith(request, "");
	    String title = MapUtils.getString(paraMap, "title");
	    int b_id = MapUtils.getIntValue(paraMap, "b_id");
	    int pageNo = MapUtils.getIntValue(paraMap, "pageNo");
	    //新添加
	    int direction = MapUtils.getIntValue(paraMap, "direction");
		String age_stage = MapUtils.getString(paraMap, "age_stage");
		int week = MapUtils.getIntValue(paraMap, "week");
	    int pageSize = 6;
    	try {
 	        if (pageNo == 0){
 	        	pageNo = 1;
 	        }
 	        //默认家长管理
 	       if (b_id == 0){
 	    	  b_id = 1;
	        }
 	        String sql = "SELECT count(*) from  db_programme.t_manage where 1=1 and b_id = "+b_id;
 	        if(direction != 0){
 				sql += " and direction = " + direction;
 			}
 			if(!StringUtils.isEmpty(age_stage)){
 				sql += " and age_stage ='" + age_stage+"'";
 			}
 			if(week != 0){
 				sql += " and week = " + week;
 			}
			if(!StringUtils.isEmpty(title)){
				sql=sql +" and title like '%" + StringUtil.getByteToString(title) + "%'";
			}
 	        @SuppressWarnings("deprecation")
			int total = this.jdbcTemplate.queryForInt(sql);
 	        //用session存所查询的总条数和一页显示的页容量
 	        HttpSession session = request.getSession();
 	        session.setAttribute("total", total);
 	        session.setAttribute("pageSize", pageSize);
 	        
 	        int startIndex = Page.getStartOfPage(pageNo, pageSize);
 	        sql = "SELECT *,date_format(create_date,'%Y-%m-%d') create_date_new from  db_programme.t_manage where 1=1 and  b_id = "+ b_id;
 	       if(direction != 0){
 				sql += " and direction = " + direction;
 			}
 			if(!StringUtils.isEmpty(age_stage)){
 				sql += " and age_stage ='" + age_stage+"'";
 			}
 			if(week != 0){
 				sql += " and week = " + week;
 			}
 	        if(!StringUtils.isEmpty(title)){
				sql += " and title like '%" + StringUtil.getByteToString(title) + "%'";
			}
 	        sql=sql+" order by create_date desc limit " + startIndex + "," + pageSize;
 	        List<Map<String, Object>> queryForList = this.jdbcTemplate.queryForList(sql);
 	        page = new Page(startIndex, total, pageSize, queryForList);
 	        return page;
 	     } catch (Exception e) {
 	    	 e.printStackTrace();
 	        logger.info("方案的分页和模糊查询失败！【错误】:manager_scheme_show方法中！");
 	        return null;
 	     }
	}
	
	
	/**
	 * @功能 	: 对方案表的删除操作
	 * @param 	: paraMap
	 * @return 	:
	 */
	public int contentform_schemamanage_delete(Map<String, Object> paraMap) {
		int id = MapUtils.getIntValue(paraMap, "id");
		try {
			String sql = "delete from db_programme.t_manage where id=?";
			int deleteNum = jdbcTemplate.update(sql, id);
			return deleteNum;
		} catch (Exception e) {
			logger.info("删除方案信息失败！【错误】:contentform_schemamanage_delete方法中！");
			return 0;
		}
	}
		
}

